package com.group.itms.routes.activities;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.group.itms.routes.R;
import com.group.itms.routes.adapters.MultiViewTypeAdapter;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.Topic;
import com.group.itms.routes.utils.Header;
import com.group.itms.routes.utils.Item;

public class TopicsActivity extends BaseActivity {

	private static final String TAG = "TopicsActivity";

	private BObject mSubject;

	public static Intent getIntent(Context c, int subjectId) {
		Intent intent = new Intent(c, TopicsActivity.class);
		intent.putExtra(OBJECT_ID, subjectId);
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int subjectId = -1;
		Bundle args = getIntent().getExtras();
		if (args != null) {
			subjectId = args.getInt(OBJECT_ID, -1);
		}
		if (subjectId != -1) {
			mSubject = getObject(subjectId);
			if (mSubject == null) {
				throw new RuntimeException("Unable to find BObject with id:"
						+ subjectId);
			}
			setupWithSubject();
		} else {
			setup();
		}
	}

	private void setup() {
		List<Topic> topics = queryForAllTopics();
		final ArrayAdapter<Topic> topicsAdapter = new ArrayAdapter<Topic>(this,
				R.layout.textview, topics);
		ListView topicsListView = new ListView(this);
		topicsListView.setAdapter(topicsAdapter);
		topicsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int topicId = topicsAdapter.getItem(position).getId();
				startObjectsActivity(topicId);
			}

		});
		setContentView(topicsListView);
	}

	private void setupWithSubject() {
		setTitle(mSubject.getName());
		List<Topic> relatedTopics = queryForRelatedTopics();
		List<Topic> otherTopics = queryForOtherTopics(relatedTopics);
		final MultiViewTypeAdapter adapter = new MultiViewTypeAdapter(this);
		adapter.add(new Header("Related topics"));
		if (relatedTopics.size() > 0) {
			adapter.addAll(toItems(relatedTopics));
		}
		adapter.add(new Header("Other topics"));
		if (otherTopics.size() > 0) {
			adapter.addAll(toItems(otherTopics));
		}
		ListView topicsListView = new ListView(this);
		topicsListView.setAdapter(adapter);
		setContentView(topicsListView);
	}

	private void startObjectsActivity(int topicId) {
		Intent intent = new Intent(TopicsActivity.this, BObjectsActivity.class);
		intent.putExtra(BObjectsActivity.TOPIC_ID, topicId);
		startActivity(intent, true);
	}

	private List<Topic> queryForRelatedTopics() {
		List<Topic> related = new ArrayList<Topic>();
		List<Topic> topics = mSubject.getTopics();
		for (Topic t : topics) {
			refreshTopic(t);
			if (t.isActive()) {
				related.add(t);
			}
		}
		return related;
	}

	private List<Topic> queryForOtherTopics(List<Topic> related) {
		List<Topic> all = queryForAllTopics();
		List<Topic> topics = new ArrayList<Topic>(all.size() - related.size());
		for (Topic tpc : all) {
			if (tpc.isActive() && !related.contains(tpc)) {
				topics.add(tpc);
			}
		}
		return topics;

	}

	private List<Item> toItems(List<Topic> topics) {
		if (topics == null) {
			return null;
		}
		List<Item> items = new ArrayList<Item>(topics.size());
		for (Topic topic : topics) {
			items.add(new Item(topic.toString(), topic));
		}
		return items;
	}

	private List<Topic> queryForAllTopics() {
		try {
			return getHelper().getTopicDao().queryForEq(Topic.IS_ACTIVE, true);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private void refreshTopic(Topic topic) {
		try {
			getHelper().getTopicDao().refresh(topic);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected String getTag() {
		return TAG;
	}
}
